package com.example.security6atguigu.controller;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

@RestController
public class IndexController {

    @GetMapping("/")
    public Map index() {
        // 获取当前用户信息
        SecurityContext context = SecurityContextHolder.getContext();
        // 获取到当前用户的认证信息
        Authentication authentication = context.getAuthentication();
        // 获取到用户身份信息
        Object principal = authentication.getPrincipal();
        // 获取到用户凭证信息[password]
        Object credentials = authentication.getCredentials();// 脱敏处理
        // 获取到用户权限信息
        Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
        // 获取到用户名称
        String name = authentication.getName();

        HashMap result = new HashMap();
        result.put("username",name);
        result.put("authorities",authorities);
        return result;
    }
}
